<?php
class Xml{
	public static function parse($xml,$node,$sub_node){
		if(!file_exists($xml)){
			Core::showmessage('The Current `'.$xml.'` Not Exists!');
		}
		return self::archieve($xml,$node,$sub_node);

	}

	/**
	 *节点归档
	 *@param $xml xml文件
	 *@param $parentNode 节点
	 *@param $subNode 子节点
	 *@return Array
	 */
	private static function archieve($xml,$parentNode,$subNode=null){
		static $reader=null;
		if(!isset($reader)){
			$reader = new XMLReader();
		}
		$reader->open($xml);

		// 游标移动到指定节点上
		while($reader->read()){
			if($reader->nodeType==XMLREADER::ELEMENT && $reader->localName==$parentNode) break;
		}

		if($subNode !== null){
			while($reader->read()){
				if($reader->nodeType==XMLREADER::ELEMENT && $reader->localName==$subNode) break;
			}
		}

		$tree = array();
		$tag = '';
		while($reader->read()){
			switch($reader->nodeType){
			case XMLREADER::ELEMENT:
				$tag = $reader->localName;
				break;
			case XMLREADER::TEXT:
				$tree[$tag]['value'] = $reader->value;
				break;
			case XMLREADER::END_ELEMENT:
				if($subNode !== null && $reader->localName == $subNode) break 2;
				if($reader->localName == $parentNode) break 2;
			}
		}
		return $tree;
	}
}
